const Actions = require('./actions');
const DB = require('app/db');
const monStorage = require('clientConfig/util/StoreData').monStorage;
const Storage = require('clientConfig/util/StoreData').Storage;
const { Toast } = SaltUI;
import { order } from 'clientConfig/util/queryurlfield';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        proj_name: '',
        folder_id: '',//根路径文件夹id
        file_count: 0
    },
    //获取文件目录列表
    onFetch: function (params, cb) {
        let t = this;
        this.data.proj_name = params.proj_name;
        if (params.folder_id)
            this.data.folder_id = params.folder_id;
        DB.File.get_filelists({
            folder_id: this.data.folder_id,
            id: Storage.get('user.id')
        })
            .then((content) => {
                this.data.file_count = content.file_count;
                let obj = {};
                let list = [];
                if (content.file_list.length > 0) {
                    for (let item of content.file_list) {
                        //{ fileName: "支付信息1.xlsx", date: "2017-09-05 08:30", fileSize: "23.5kB", uploader: "李莉" },
                        if (!item.file_folder) {
                            obj.url = item.file_id;//文件id
                            obj.fileName = item.file_name;//文件名称
                            obj.file_type = item.file_type;//文件类型
                            obj.fileSize = parseInt(item.file_size / 1024) + 'KB';//文件大小
                            obj.uploader = item.file_uper;//文件上传者id
                            obj.date = item.file_uptime;//文件上传时间
                            obj.proj_name = item.proj_name//文件所属项目名称
                            obj.file_version = item.file_version//文件版本
                            obj.file_uper_name = item.file_uper_name//文件上传人                        
                            obj.folder_id = item.folder_id;//所属文件夹id
                            obj.folder = item.file_folder;//是否为文件夹false
                        } else {
                            obj.folder = item.file_folder;//是否为文件夹true
                            obj.process_id = item.process_id;//关联的审批单-根目录时含有，其他可省略
                            obj.uploader = item.file_uper;//文件上传者id
                            obj.file_uper_name = item.file_uper_name//文件上传人
                            // obj.folder_name = item.folder_name;//文件夹名称
                            obj.fileName = item.folder_name;//文件夹名称
                            obj.date = item.file_uptime;//文件夹创建时间
                            obj.folder_id = item.folder_id;//文件夹id
                            /*---------------*/
                            obj.fileSize = '---'
                            switch (obj.fileName) {
                                case "招标":
                                    obj.value = 1;
                                    break;
                                case "实施":
                                    obj.value = 2;
                                    break;
                                case "验收":
                                    obj.value = 3;
                                    break;
                                case "维护":
                                    obj.value = 4;
                                    break;
                            }
                        }
                        list.push(obj)
                        obj = {};
                    }
                }
                if (list.length == 4) {
                    list.sort(order)
                }
                this.data.data = list;
                cb && cb(params.pathname, params.folder_id, list);
                t.updateComponent();
            })
            .catch((error) => {

            });
    },
    //上传文件
    onUpFiles: function (params, data) {
        let t = this;
        let folder_id = params.folder_id;
        $.ajax({
            type: "post",
            url: "/its/upload_file?proj_name=" + params.name + "&folder_id=" + folder_id + "&file_uper=" + Storage.get('user.id') + "&file_uper_name=" + monStorage.get('user.nickname'),
            data: data,
            processData: false,
            contentType: false,
            success: function (result) {
                Toast.show({
                    type: 'success',
                    content: "文件上传成功",
                });
                let obj = {};
                obj.file_type = result.content.file_type;//文件类型
                obj.proj_name = result.content.proj_name//文件所属项目名称                
                obj.file_uper_name = result.content.file_uper_name//文件上传人                        
                obj.fileName = result.content.file_name;
                obj.file_version = result.content.file_version
                obj.date = result.content.file_uptime;
                obj.fileSize = parseInt(result.content.file_size / 1024) + 'KB';
                obj.uploader = result.content.file_uper
                obj.url = result.content.file_id;
                obj.folder = false;
                obj.file_id = result.content.file_id;
                obj.folder_id = result.content.folder_id;
                t.data.data.push(obj);
                t.bindFilePermission(result.content.file_id, Storage.get('user.id'));
                t.updateComponent();
            }
        })
    },
    //下载文件
    onDownload: function (params) {
        let t = this;

        // params[key].value.url = "BD5D5595-5FD9-49FA-BA47-FF3ACECB3C76"
        if (!window.isBp) {
            for (let key in params) {
                DingTalkPC.biz.util.downloadFile({
                    url: "http://120.55.85.1:8091/its/download_file?file_id=" + params[key].value.url, //要下载的文件的url
                    name: params[key].value.fileName, //定义下载文件名字
                    onProgress: function (msg) {
                        // 文件下载进度回调
                    },
                    onSuccess: function (result) {
                        if (result) {
                            Toast.show({
                                type: 'success',
                                content: "文件下载成功",
                            });
                        } else {
                            Toast.show({
                                type: result,
                                content: "文件下载未成功",
                            });
                        }
                    },
                    onFail: function () {
                    }
                })
            }
        } else {
            // var a = document.createElement('a');
            // a.href = "http://120.55.85.1:8091/its/download_file?file_id=" + params[key].value.url;
            // a.target = '_blank'
            // document.body.appendChild(a);
            // a.click();
            // a.remove()

            for (let key in params) {
    
                DB.File.download_file({
                    folder_id: params[key].value.url
                })
                    .then((content) => { })
                    .catch((error) => { });
            }

        }



    },
    //删除文件
    onDelete: function (params, cb) {
        let t = this;
        for (let key in params) {
            if (!params[key].value.folder) {
                DB.File.delete_file({ file_id: params[key].value.url, id: Storage.get('user.id') })
                    .then((content) => {
                        if (!content) {
                            Toast.show({
                                type: 'success',
                                content: `${params[key].value.fileName}文件删除成功`,
                            });
                        }
                    }).then(() => {
                        const fn = (item) => {
                            return params[key].value.url != item.url;
                        }
                        t.data.data = t.data.data.filter(fn);
                        //#warning 回调进去t.data.data数据已都变n次
                        cb && cb();
                        t.updateComponent();
                    })
                    .catch((error) => {

                    })
            }
        }
    },
    //创建文件夹
    onCreateFilePath: function ({ parent_folder_id, folder_name }, cb) {
        let t = this;
        DB.File.create_file_path({
            parent_folder_id,
            folder_name,
            file_uper: Storage.get('user.id'),
            file_uper_name: Storage.get('user.nickname')
        })
            .then((content) => {
                // debugger
                let obj = {};
                obj.folder = true;//是否为文件夹true
                obj.process_id = "";//关联的审批单-根目录时含有，其他可省略
                obj.uploader = Storage.get('user.id');//文件上传者id
                obj.file_uper_name = Storage.get('user.nickname')//文件上传人
                // obj.folder_name = item.folder_name;//文件夹名称
                obj.fileName = folder_name;//文件夹名称
                obj.date = content.file_uptime;//文件夹创建时间#warning
                obj.folder_id = content.folder_id;//文件夹id#warning
                /*---------------*/
                obj.fileSize = '---';
                // debugger
                t.data.data.unshift(obj);
                cb && cb();
                t.updateComponent();
            })
            .catch((error) => {

            });
    },
    //重命名
    onRename: function (params) {
        let folder_name = params.folder_name;
        let folder_id = params.folder_id;
        DB.File.rename_file_path({
            folder_id,
            folder_name,
            file_uper: Storage.get('user.id')
        })
            .then((content) => {
                if (!content) {
                    Toast.show({
                        type: 'success',
                        content: "修改成功",
                    });
                }

            })
            .catch((error) => {

            });

    },
    //删除文件夹
    onDeleteFilePath: function (params, cb) {
        // let n = 0;
        let t = this;
        let folder_id = '';
        for (let key in params) {
            folder_id = params[key].value.folder_id;
            if (folder_id) {
                DB.File.delete_file_path({
                    folder_id,
                    file_uper: Storage.get('user.id')
                })
                    .then((content) => {
                        if (!content) {
                            Toast.show({
                                type: 'success',
                                content: `${params[key].value.fileName}文件夹删除成功`,
                            });
                        }
                    }).then(() => {
                        const fn = (item) => {
                            return params[key].value.folder_id == item.folder_id;
                        }
                        let num = t.data.data.findIndex(fn);
                        if (num != -1) {
                            t.data.data.splice(num, 1);
                        }
                        //#warning 回调进去t.data.data数据已都变n次
                        cb && cb();
                        t.updateComponent();
                    })
                    .catch((error) => {
                        if (error.errcode == 2) {
                            Toast.show({
                                type: 'error',
                                content: error.message,
                            });
                        }

                    })
            }
        }
    },
    onSearch: function (params, cb) {
        let t = this;
        if (!params.file_name) return;
        DB.File.get_file({
            id: params.id,
            proj_name: params.proj_name,
            key_word: params.file_name
        })
            .then((content) => {
                let data = content.file_info
                let obj = {};
                let list = [];
                if (data.length > 0) {
                    for (let item of data) {
                        if (!item.file_folder) {
                            obj.url = item.file_id;//文件id
                            obj.fileName = item.file_name;//文件名称
                            obj.file_type = item.file_type;//文件类型
                            obj.fileSize = parseInt(item.file_size / 1024) + 'KB';//文件大小
                            obj.uploader = item.file_uper;//文件上传者id
                            obj.date = item.file_uptime;//文件上传时间
                            obj.proj_name = item.proj_name//文件所属项目名称
                            obj.file_version = item.file_version//文件版本
                            obj.file_uper_name = item.file_uper_name//文件上传人                        
                            obj.folder_id = item.folder_id;//所属文件夹id
                            obj.folder = item.file_folder;//是否为文件夹false
                        } else {
                            obj.folder = item.file_folder;//是否为文件夹true
                            obj.process_id = item.process_id;//关联的审批单-根目录时含有，其他可省略
                            obj.uploader = item.file_uper;//文件上传者id
                            obj.file_uper_name = item.file_uper_name//文件上传人
                            // obj.folder_name = item.folder_name;//文件夹名称
                            obj.fileName = item.folder_name;//文件夹名称
                            obj.date = item.file_uptime;//文件夹创建时间
                            obj.folder_id = item.folder_id;//文件夹id
                            /*---------------*/
                            obj.fileSize = '---'
                        }
                        list.push(obj)
                        obj = {};
                    }
                }
                this.data.data = list;
                t.updateComponent();
            })
            .catch((error) => {

            });
    },
    //绑定文件参与者的逻辑
    bindFilePermission: function (file_id, originator_user_id) {
        let unionids = [originator_user_id];
        const set = new Set(unionids);
        let data = set;
        let participant = [...data];
        if (file_id) {
            DB.Permission.bind_file_permission({ file_id, participant: JSON.stringify(participant) })
                .then((content) => {
                })
                .catch((error) => {
                    Toast.show({
                        type: "error",
                        content: "绑定文件参与者未成功"
                    })
                });
        }
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
